// 小孩分糖果
function childCandy(n1, n2) {
    n1 = n1.sort((a, b) => a - b);  // 饼干
    n2 = n2.sort((a, b) => a - b);  // 孩子
    let num = 0;
    let index = 0;
    for (let i = 0; i < n1.length; i++) {
        if (i < n2.length && n1[i] > n2[index]) {  // 不能等于
            num++;
            index++;
        }
    }
    return num;
}

// 摆动序列
// 通过从原始序列中删除一些（也可以不删除）元素来获得子序列，剩下的元素保持其原始顺序。
function wingmaxLength(arr) {
    let prediff = arr[0] - arr[1]
    let count = prediff == 0 ? 1 : 2;
    let precount = prediff == 0 ? 1 : 2;
    for (let i = 2; i < arr.length; i++) {
        let curdiff = arr[i] - arr[i + 1]
        if (prediff > 0 && curdiff < 0 || prediff < 0 && curdiff > 0) {
            count++;
            prediff = curdiff
        } else if (prediff == 0 || curdiff == 0) {
            count = Math.max(count, precount)
            precount = count
            prediff = curdiff
        }
    }
    return count
}


var wiggleMaxLength = function (nums) {
    if (nums.length < 2) {
        return nums.length;
    }

    let prediff = nums[0] - nums[1];
    let count = prediff === 0 ? 1 : 2;

    for (let i = 0; i < nums.length - 1; i++) {
        let diff = nums[i] - nums[i + 1];
        if ((diff > 0 && prediff <= 0) || (diff < 0 && prediff >= 0)) {
            count++;
            prediff = diff
        }
    }
    return count
};

// console.log(wingmaxLength([1, 7, 7, 7, 7, 5]))
// console.log(wiggleMaxLength([1, 7, 7, 7, 2, 5]))


// 移除k个数字
// num = "1432219", k = 3
// 比较当前位最左
function removeKdigits(nums, k) {
    let stack = []
    for (const val of nums) {
        while (k && stack.length && stack[stack.length - 1] > val) {
            stack.pop();
            k--
        }
        stack.push(val)
    }
    let res = stack.join("");
    if (res == 0) return '0';
    while (stack[0] == 0) {
        stack.shift()
    }
    // console.log(stack)
    // k没有减小的话 要将删除位删满
    return stack.join("").substring(0, stack.length - k)
}

console.log(removeKdigits("1432219",3))

// 跳跃游戏
// 一个数组 最大跳跃能到最后一个 哪种情况 会不能跳到最后 每一的最大位置 看最大位置+最后一位值是否>length
function jumpGame(arr) {
    // 赞暂存前面最长
    let maxlen = 0;
    for (let i = 0; i < arr.length; i++) {
        if (maxlen < i) return false;
        maxlen = Math.max(i + arr[i], maxlen);
        if (maxlen > length - 1) return true
    }
}

// 射击气球
// 气球是一个二位数组，判断哪些能相交，不能的
function shotBall(balls) {
    balls.sort((a, b) => a[1] - b[1]);
    let end = balls[0][1];
    let count = 1;
    for (let i = 0; i < balls.length; i++) {
        if (balls[i][0] > end) {
            count++;
            end = balls[i][1]
        }
    }
    return count
}

console.log(shotBall([[10, 16], [2, 8], [1, 6], [7, 12]]))